package cn.howxu.yichen_ovo.common.effect;

import cn.howxu.yichen_ovo.init.ModEffects;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;

/**
 * @description: TODO
 * @author: HowXu
 * @date: 2025/10/5 19:11
 */
public class YiChen extends MobEffect {
    public YiChen() {
        super(MobEffectCategory.BENEFICIAL,0x8A2BE2);
    }

    @Override
    public boolean isDurationEffectTick(int pDuration, int pAmplifier) {
        // 二分之一秒执行一次
        return pDuration % 10 == 0;
    }

    @Override
    public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
        super.applyEffectTick(pLivingEntity, pAmplifier);
        if (pLivingEntity instanceof Player pPlayer){

            // 小于520就补到520
            var effect = pPlayer.getEffect(ModEffects.YICHEN.get());
            if (effect != null && effect.getDuration() < 320 * 20 + 1){
                pPlayer.addEffect(new MobEffectInstance(ModEffects.YICHEN.get(),320 * 20 + 1,1,true,false));
            }
            // 允许飞行
            pPlayer.getAbilities().mayfly = true;

            // 治疗效果
            if (pPlayer.getHealth() != pPlayer.getMaxHealth()){
                pPlayer.heal(pPlayer.getMaxHealth());
            }

            // 夜视 抗火和抗性提升
            pPlayer.addEffect(new MobEffectInstance(MobEffects.NIGHT_VISION,320 * 20 + 1,5,true,false));
            pPlayer.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE,320 * 20 + 1,5,true,false));
            pPlayer.addEffect(new MobEffectInstance(MobEffects.DAMAGE_RESISTANCE,320 * 20 + 1,15,true,false));

            // 粒子效果 头顶冒爱心
            if (!pPlayer.level().isClientSide) {
                // 在实体周围随机位置生成爱心粒子
                ServerLevel serverLevel = (ServerLevel) pPlayer.level();
                double x = pPlayer.getX() + (pPlayer.getRandom().nextDouble() - 0.5) * 2; // 实体周围X坐标偏移
                double y = pPlayer.getY() + pPlayer.getEyeHeight() + (pPlayer.getRandom().nextDouble() - 0.5); // 实体周围Y坐标偏移
                double z = pPlayer.getZ() + (pPlayer.getRandom().nextDouble() - 0.5) * 2; // 实体周围Z坐标偏移

                // 发送粒子数据包到客户端，让粒子实际显示出来。
                // 第一个参数是粒子类型，这里使用原版的HEART粒子。
                // 后续参数是粒子的坐标。
                serverLevel.sendParticles(ParticleTypes.HEART, pPlayer.getX(), pPlayer.getY() + 1.8d, pPlayer.getZ(), 2, 0, 0, 0, 0);
            }
        }
    }
}
